﻿using Engine.DynamicObjectManager;
using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using WorldRacing2.Scenario.WR2TerrainLoader;

namespace WorldRacing2.Configuration.ObjectsProperties
{
    internal partial class ObjectPropertyControl : UserControl
    {
        private readonly WorldRacing2ConfigurationData configurationData;

        #region Private Fields

        private bool duringUpdate = false;

        #endregion Private Fields

        #region Public Constructors

        public ObjectPropertyControl(WorldRacing2ConfigurationData configurationData)
        {
            InitializeComponent();
            this.fileTreeView_Objects.SearchPattern = "*.Mox|*.Tree";
            this.configurationData = configurationData;
            SetSceneryObjectFile(this.configurationData.SceneryObjectPropertiesFile);
            SetObjectRootfolder();
        }

        #endregion Public Constructors



        #region Public Methods

        public void SetControls(BaseObjectProperties property)
        {
            this.duringUpdate = true;
            switch (property.CollisionModel)
            {
                case EnumBaseObjectCollisionModel.Bake:
                    this.radioButton_ColBake.Checked = true;
                    break;

                case EnumBaseObjectCollisionModel.HeavyBox:
                    this.radioButton_ColHeavyBox.Checked = true;
                    break;

                case EnumBaseObjectCollisionModel.LightBox:
                    this.radioButton_ColLightbox.Checked = true;
                    break;

                case EnumBaseObjectCollisionModel.Pylon:
                    this.radioButton_ColPylon.Checked = true;
                    break;

                case EnumBaseObjectCollisionModel.Self:
                    this.radioButton_ColSelf.Checked = true;
                    break;

                default:
                    throw new Exception("unknown collision type!");
            }

            switch (property.BaseObjectType)
            {
                case EnumBaseObjectType.Propeller:
                    this.radioButton_Propeller.Checked = true;
                    break;

                case EnumBaseObjectType.Sprite3:
                    this.radioButton_Sprite.Checked = true;
                    break;

                case EnumBaseObjectType.Sprite8:
                    this.radioButton_Sprite8.Checked = true;
                    break;

                case EnumBaseObjectType.Static:
                    this.radioButton_Static.Checked = true;
                    break;

                case EnumBaseObjectType.Sway:
                    this.radioButton_Sway.Checked = true;
                    break;

                case EnumBaseObjectType.Tree:
                    this.radioButton_Tree.Checked = true;
                    break;

                case EnumBaseObjectType.unknown:
                    this.radioButton_Unknown5.Checked = true;
                    break;

                case EnumBaseObjectType.Wave:
                    this.radioButton_Wave.Checked = true;
                    break;

                case EnumBaseObjectType.XTree:
                    this.radioButton_XTree.Checked = true;
                    break;

                default:
                    throw new Exception("Unknow object type!");
            }

            this.numericUpDown_Weight.Value = property.Weight;
            this.comboBox_FallSound.Text = property.FallSound;
            this.comboBox_HitSound.Text = property.CollisionSound;

            this.duringUpdate = false;
        }

        public void SetObjectRootfolder()
        {
            this.fileTreeView_Objects.SetStartFolder(Application.StartupPath + "\\MoxObjects");
            // this.fileTreeView_Objects.SetStartFolder(folder);
        }

        private void SetSceneryObjectFile(SceneryObjectPropertiesFile sceneryObjectPropertiesFile)
        {
            this.configurationData.SceneryObjectPropertiesFile = sceneryObjectPropertiesFile;
            string[] fallSounds = this.configurationData.SceneryObjectPropertiesFile.GetAllKnownFallSounds();
            string[] hitSounds = this.configurationData.SceneryObjectPropertiesFile.GetAllKnownHitSounds();

            for (int i = 0; i < fallSounds.Length; i++)
            {
                // Add known sounds to the comboboxes.
                if (!this.comboBox_FallSound.Items.Contains(fallSounds[i]))
                    this.comboBox_FallSound.Items.Add(fallSounds[i]);
            }

            for (int i = 0; i < hitSounds.Length; i++)
            {
                // Add known sounds to the comboboxes.
                if (!this.comboBox_HitSound.Items.Contains(hitSounds[i]))
                    this.comboBox_HitSound.Items.Add(hitSounds[i]);
            }
        }

        #endregion Public Methods

        #region Private Methods

        private void button_Import_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "QAD-File (*.qad)|*.qad";

            if (ofd.ShowDialog() == DialogResult.OK)
            {
                QADFile qadFile = QADFile.FromFile(ofd.FileName);

                string fileNameObjectProperties = Application.StartupPath + "\\objectProperties.bin";
                this.configurationData.SceneryObjectPropertiesFile = Engine.DynamicObjectManager.SceneryObjectPropertiesFile.FromFile(fileNameObjectProperties);
                for (int i = 0; i < qadFile.objectProperties.Count; i++)
                {
                    if (qadFile.objectProperties[i].Name.Contains("T\\"))
                    {
                        int length = qadFile.objectProperties[i].Name.Length;
                        this.configurationData.SceneryObjectPropertiesFile.AddObjectPropertie(qadFile.objectProperties[i].Name.Substring(2, length - 2) + ".tree", qadFile.objectProperties[i]);
                    }
                    else
                        this.configurationData.SceneryObjectPropertiesFile.AddObjectPropertie(qadFile.objectProperties[i].Name + ".mox", qadFile.objectProperties[i]);
                }
                this.configurationData.SceneryObjectPropertiesFile.ToFile(fileNameObjectProperties);
            }
        }

        private void button_Save_Click(object sender, EventArgs e)
        {
            if (File.Exists(this.fileTreeView_Objects.SelectedFullPath))
            {
                string filename = Path.GetFileName(this.fileTreeView_Objects.SelectedFullPath);
                BaseObjectProperties prop = GetCurrentObjectProperties();

                if (!this.comboBox_FallSound.Items.Contains(prop.FallSound))
                    this.comboBox_FallSound.Items.Add(prop.FallSound);

                if (!this.comboBox_HitSound.Items.Contains(prop.CollisionSound))
                    this.comboBox_HitSound.Items.Add(prop.CollisionSound);

                this.configurationData.SceneryObjectPropertiesFile.AddObjectPropertie(filename, prop);
                this.configurationData.SceneryObjectPropertiesFile.ToFile(Application.StartupPath + "\\objectProperties.bin");

                // Just Update the info controls.
                fileTreeView_Objects_InexChanged();
            }
        }

        private void fileTreeView_Objects_InexChanged()
        {
            if (File.Exists(this.fileTreeView_Objects.SelectedFullPath))
            {
                string filename = Path.GetFileName(this.fileTreeView_Objects.SelectedFullPath);
                BaseObjectProperties properties;
                if (this.configurationData.SceneryObjectPropertiesFile.TryToGetValue(filename, out properties))
                {
                    SetControls(properties);
                    this.label_DataInfo.ForeColor = Color.Green;
                    this.label_DataInfo.Text = properties.Name;
                }
                else
                {
                    this.label_DataInfo.ForeColor = Color.Red;
                    this.label_DataInfo.Text = "No Data stored!!";
                }
            }
        }

        private BaseObjectProperties GetCurrentObjectProperties()
        {
            string nameOfPropertie = Path.GetFileNameWithoutExtension(this.fileTreeView_Objects.SelectedFullPath);
            BaseObjectProperties objectProperties = new BaseObjectProperties(nameOfPropertie);

            // Collision Model
            if (this.radioButton_ColBake.Checked)
                objectProperties.CollisionModel = EnumBaseObjectCollisionModel.Bake;
            else if (this.radioButton_ColHeavyBox.Checked)
                objectProperties.CollisionModel = EnumBaseObjectCollisionModel.HeavyBox;
            else if (this.radioButton_ColLightbox.Checked)
                objectProperties.CollisionModel = EnumBaseObjectCollisionModel.LightBox;
            else if (this.radioButton_ColPylon.Checked)
                objectProperties.CollisionModel = EnumBaseObjectCollisionModel.Pylon;
            else if (this.radioButton_ColSelf.Checked)
                objectProperties.CollisionModel = EnumBaseObjectCollisionModel.Self;

            // OLbject type
            if (this.radioButton_Propeller.Checked)
                objectProperties.BaseObjectType = EnumBaseObjectType.Propeller;
            else if (this.radioButton_Sprite.Checked)
                objectProperties.BaseObjectType = EnumBaseObjectType.Sprite3;
            else if (this.radioButton_Sprite8.Checked)
                objectProperties.BaseObjectType = EnumBaseObjectType.Sprite8;
            else if (this.radioButton_Static.Checked)
                objectProperties.BaseObjectType = EnumBaseObjectType.Static;
            else if (this.radioButton_Sway.Checked)
                objectProperties.BaseObjectType = EnumBaseObjectType.Sway;
            else if (this.radioButton_Tree.Checked)
                objectProperties.BaseObjectType = EnumBaseObjectType.Tree;
            else if (this.radioButton_Unknown5.Checked)
                objectProperties.BaseObjectType = EnumBaseObjectType.unknown;
            else if (this.radioButton_Wave.Checked)
                objectProperties.BaseObjectType = EnumBaseObjectType.Wave;
            else if (this.radioButton_XTree.Checked)
                objectProperties.BaseObjectType = EnumBaseObjectType.XTree;

            // Other properties

            objectProperties.Weight = (int)this.numericUpDown_Weight.Value;
            objectProperties.FallSound = (string)this.comboBox_FallSound.Text;
            objectProperties.CollisionSound = (string)this.comboBox_HitSound.Text;

            return objectProperties;
        }

        private void Properties_Changed(object sender, EventArgs e)
        {
            if (!this.duringUpdate)
            {
                if (File.Exists(this.fileTreeView_Objects.SelectedFullPath))
                {
                    // Live Update not used atm.
                    // MessageBox.Show("Properies Changed for:\n" + Path.GetFileName(this.fileTreeView_Objects.SelectedFullPath));
                }
            }
        }

        #endregion Private Methods
    }
}